import numpy as np
import pandas as pd
import pyecharts
import sqlite3

if __name__ == '__main__':

    db_path = '../db.sqlite3'

else:

    db_path = 'db.sqlite3'

class UserAna():

    def __init__(self):

        self.table_name = 'users_csv'

        self.conn = sqlite3.connect(db_path)

        self.df = pd.read_sql('SELECT * FROM users_csv', self.conn)

    def echarts_bar(self, series, bar_name = 'test', name = 'test_name' ):

        bar = pyecharts.Bar(bar_name)

        asix0 = series.index

        asix1 = series.values.ravel()

        bar.add(name, asix0, asix1, is_label_show=True, is_datazoom_show=True)

        return bar

    def many_echarts_bar(self, multiindex_df,bar_name = 'test'):

        #传入一个many_echarts_bar(Number_of_genders_per_occupation, 'F'),生成一个每个职业男生图

        bar = pyecharts.Bar(bar_name)

        asix = multiindex_df.index.levels[1]

        # name = multiindex_df.index.levels[1]

        for i in asxi:

            df1 = multiindex_df.loc[(slice(None),i),:]
            axis1 = df1.values.ravel()

            axis0 = df1.index.levels[0]

            bar.add(i, axis0, axis1, is_label_show=True, is_datazoom_show=True,is_stack = True)

        return bar

        

        


    def Number_of_genders_per_occupation(self):

        df1 = self.df.groupby(['occupation','gender']).count()

        df1['count'] = df1.user_id

        return df1.drop(df1.columns[:-1] ,axis = 1)

    #def bar_Number_of_genders_per_occupation(self):







    


    

UserAna_obj = UserAna()

if __name__ == '__main__':

    x = UserAna()

    




















    


        
